package development;

import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.TimerListener;
import state.font.FontManager;
import state.font.ImageText;
import state.util.GameColor;
import state.util.Hashtable;

public class FrmLogo extends GameForm implements TimerListener {
  private GameImage loadingImage1 = null;
  private GameImage loadingImage2 = null;
// private Image logoImage;

  private ImageText vnText = null;

  public String message = null;
  public String message1 = null;

  private int count = 0;

  private static GameForm singleton = null;

  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmLogo();
    }
    return singleton;
  }

  public void init(Hashtable hashtable) {
    loadingImage1 = GameImage.createImage1(GameConstants.CORE_IMAGE_FOLDER + "/loading_1.png");
    loadingImage2 = GameImage.createImage1(GameConstants.CORE_IMAGE_FOLDER + "/loading_2.png");
    vnText = FontManager.getFont(FontManager.FONT_SIZE_8);

    setTimerListener(this);
    message = "Đang kết nối, xin vui lòng chờ";
    message1 = "";
  }

  public void draw(Graphics g) {
    g.setColor(0x4F0202);
    g.fillRect(0, 0, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT);

    int index = count % 11;
    int x1 = 0;
    int y1 = 0;
    if (GameConstants.IS_240x320_SCREEN) {
      vnText.drawString2(g, message, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 210, GameConstants.TOP_HCENTER_ANCHOR);
      vnText.drawString2(g, message1, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 264, GameConstants.TOP_HCENTER_ANCHOR);
      x1 = 62;
      y1 = 247;
    } else {
      vnText.drawString2(g, message, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 140, GameConstants.TOP_HCENTER_ANCHOR);
      vnText.drawString2(g, message1, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 193, GameConstants.TOP_HCENTER_ANCHOR);
      x1 = 101;
      y1 = 172;
    }

    for (int i = 1; i < 11; i++) {
      if (i <= index) {
        g.drawImage(loadingImage1, x1 + (loadingImage1.getWidth() + 1) * (i - 1), y1, GameConstants.TOP_LEFT_ANCHOR);
      } else {
        g.drawImage(loadingImage2, x1 + (loadingImage1.getWidth() + 1) * (i - 1), y1, GameConstants.TOP_LEFT_ANCHOR);
      }
    }
  }

  public void doTask() {
    count++;
    if (count > 2) {
      GameGlobal.nextState(FrmMenu.instance(), null);
    }
  }

  public void destroy() {
    loadingImage1 = null;
    loadingImage2 = null;
    vnText = null;
  }
}
